﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.BlackApart.Data;
using blackhouse.BlackApart.Business.Reservations;
using System.Globalization;

namespace blackhouse.BlackApart.Business.MailTemplate {
	public class DaMailTemplate : IMailTemplate {

		#region Fields

		private ImpMailTemplate instance;
		private IMailTemplateDataAccess dataAccess;

		#endregion

		#region Properties

		public int Id {
			get { return this.instance.Id; }
			private set { this.instance.Id = value; }
		}

		public string MailTitle {
			get { return this.instance.MailTitle; }
			private set { this.instance.MailTitle = value; }
		}

		public string TemplateControl {
			get { return this.instance.TemplateControl; }
			private set { this.instance.TemplateControl = value; }
		}

		public DateTime LastUpdateTime {
			get { return this.instance.LastUpdateTime; }
			private set { this.instance.LastUpdateTime = value; }
		}

		#endregion

		#region Constructors

		public DaMailTemplate(IMailTemplateDataAccess dataAccess, int mailTemplateId) {
			this.dataAccess = dataAccess;
			this.instance = this.ReadInstance(mailTemplateId);
			if (this.instance == null)
				this.instance = new ImpMailTemplate();
		}

		#endregion

		#region Methods

		private ImpMailTemplate ReadInstance(int templateId) {
			MailTemplateData data = this.dataAccess.GetMailTemplate(templateId, CultureInfo.CurrentCulture.Name);
			if (data.Id == templateId)
				return this.Convert(data);
			else return null;
		}

		private ImpMailTemplate Convert(MailTemplateData data) {
			ImpMailTemplate template = new ImpMailTemplate();
			template.Id = data.Id;
			template.TemplateControl = data.TemplateContent;
			template.LastUpdateTime = data.LastUpdateTime;
			template.MailTitle = data.Title;
			return template;
		}

        public string GetMailTemplateUrlForReservation(IWebReservation reservation) {
			return this.instance.GetMailTemplateUrlForReservation(reservation);
		}

		#endregion

	}
}
